package com.example.dbg.common.interceptor;

import com.example.dbg.model.po.CuUser;
import com.example.dbg.service.ICuUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @user DBG_ZXX
 * @date 2018/10/29 9:10
 * @desc
 **/
@Component
public class AuthorizationInterceptor implements HandlerInterceptor {

    Logger logger = LoggerFactory.getLogger(AuthorizationInterceptor.class);

    @Autowired
    ICuUserService cuUserService;
/*
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("------ 使用拦截器");
//        Object userName = request.getSession().getAttribute("loginUserName");
//        if(StringUtils.checkValNull(userName)){
        if(true){
            logger.info("------ 未通过转到login");
//            response.sendRedirect("login") ;
//            request.getRequestDispatcher("/login").forward(request,response);
//            response.sendRedirect(request.getContextPath() + "/login") ;
            return true;
        }
//        return true;
//        if(StringUtils.isEmpty(userName)){
//            request.setAttribute("msg","您没有操作权限");
//            request.getRequestDispatcher("/index.html").forward(request,response);
//            return false;
//        }
        logger.info("------ true");
        return false;
    }*/

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String url = request.getRequestURI();
        System.out.println(url);
        if("/login".equals(request.getRequestURI()) || "/error".equals(request.getRequestURI()) ||
                "/loginIn".equals(request.getRequestURI())){
            logger.info("------ login不拦截");
            return true;
        }
        HttpSession session = request.getSession();
        String  loginName = (String)session.getAttribute("loginName");
        int cuUserIdOfSession = (int)session.getAttribute("cuUserId");
        System.out.println("loginName:"+loginName);
        // 如果请求list页面的 Id 与 数据库对象查到的Id不相等 则拦截不通过
        System.out.println(url.substring(0,url.lastIndexOf("/")+1));
        if ("/pwd/pwdList/".equals(url.substring(0,url.lastIndexOf("/")+1))){
            int cuUserId = Integer.parseInt(url.substring(url.lastIndexOf("/")+1,url.length()));
            System.out.println(cuUserId);
            CuUser cuUser = cuUserService.selectById(cuUserId);
            if(StringUtils.checkValNotNull(cuUser) && cuUser.getId().equals(cuUserIdOfSession)){
                logger.info("1111");
                return true;
            }else {
                return false;
            }
        }
        if (StringUtils.checkValNull(loginName)){
            logger.info("------ 未通过转到login");
            System.out.println(request.getContextPath());
            String path = request.getContextPath();
            System.out.println(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/");
            response.sendRedirect(request.getContextPath()+"/login") ;

            return false;
        }else {
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
